package com.googlecode.delivery.core.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.googlecode.delivery.model.Environment;

/**
 * Helper for loading / saving Environment files.
 * 
 * @author kst
 */
public class EnvironmentHelper {
	private static final Logger LOGGER = Logger
			.getLogger(EnvironmentHelper.class);

	/** Default token separator : <code>@</code> */
	public static String DEFAULT_TOKEN_SEPARATOR = "@";

	private static String tokenSeparator = null;

	/**
	 * Java System property for configuring token separator :
	 * <code>delivery.token.separator</code>
	 */
	public static String TOKEN_SEPARATOR_SYSTEM_PROPERTY_NAME = "delivery.token.separator";

	/** Factory pattern. */
	private EnvironmentHelper() {
	}

	/**
	 * @param sourcePath
	 * @return
	 * @throws IOException
	 */
	public static Environment create(File envProperties) throws IOException {
		LOGGER.debug("Creating environement from file="
				+ envProperties.getAbsolutePath());
		String envName = envProperties.getName();
		Environment env = new Environment(envName);
		env.setEnvFile(envProperties);
		FileInputStream fis = new FileInputStream(envProperties);
		env.load(fis);
		return env;
	}

	public static String getTokenSeparator() {
		if (tokenSeparator == null) {
			String systemSettings = System
					.getProperty("delivery.token.separator");
			tokenSeparator = (systemSettings != null) ? systemSettings
					: DEFAULT_TOKEN_SEPARATOR;
		}
		return tokenSeparator;
	}

	public static void setTokenSeparator(String ts) {
		tokenSeparator = ts;
	}

	/**
	 * @param key
	 * @return
	 */
	public static String getTokenizedKey(String key) {
		return getTokenSeparator() + key + getTokenSeparator();
	}

	/**
	 * @return
	 */
	public static Collection<String> getTokenizedKeys(Environment environment) {
		Collection<String> tokenizedKeys = new HashSet<String>();
		Map<String, String> propertyMap = environment.toMap();
		for (Iterator<String> iterator = propertyMap.keySet().iterator(); iterator
				.hasNext();) {
			String key = iterator.next();
			String tokenizedKey = EnvironmentHelper.getTokenizedKey(key);
			tokenizedKeys.add(tokenizedKey);
		}
		return tokenizedKeys;
	}

	/**
	 * @param env
	 * @param outputDir
	 * @throws DeliveryException
	 * @throws IOException
	 * 
	             public static void saveEnvironment(Environment env, File
	 *             outputDir) throws DeliveryException, IOException { if
	 *             (!outputDir.exists()) { outputDir.mkdirs(); } if
	 *             (env.getEnvFile() == null) { throw new
	 *             DeliveryException("Environment file not set ", null); }
	 *             LOGGER .info("Storing environment " + env.getName() + " to "
	 *             + outputDir); BufferedReader br = new BufferedReader(new
	 *             InputStreamReader( new FileInputStream(env.getEnvFile())));
	 *             BufferedWriter bw = new BufferedWriter(new
	 *             OutputStreamWriter( new FileOutputStream(outputDir + "/" +
	 *             DeploymentUnitFactory.ENV_FILE_NAME)));
	 * 
	 *             try { String line = null; while ((line = br.readLine()) !=
	 *             null) { bw.write(line); bw.newLine(); } } finally {
	 *             bw.flush(); bw.close(); br.close(); } }
	 */

}
